//
//  VideoTableViewCell.m
//  NEWS_W_Y_S
//
//  Created by lanou3g on 15/10/14.
//  Copyright © 2015年 lanou3g.jake. All rights reserved.
//

#import "VideoTableViewCell.h"
#import "VideoModel.h"
#import "UIImageView+WebCache.h"
#import "AudiovisualViewController.h"
#import "MBProgressHUD.h"

@interface VideoTableViewCell()<UIGestureRecognizerDelegate>

@property(nonatomic,strong) NSURL *urling;

@property(nonatomic,strong) UIWebView *webView;

@property(nonatomic,assign) BOOL isPlay;

@property(nonatomic,assign) BOOL isPlayer;

@property(nonatomic,strong) MBProgressHUD *progressHUD;

@end

@implementation VideoTableViewCell

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    if ([NSStringFromClass([touch.view class]) isEqualToString:@"UITableViewCellContentView"]) {
        return NO;
    }else{
        return YES;
    }
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self layoutView];
        self.moviePlayer = [[MPMoviePlayerController alloc] init];
    }
    return self;
}


-(void)layoutView{
    
    _videoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 200)];
    //_videoImageView.backgroundColor = [UIColor grayColor];
    _videoImageView.userInteractionEnabled = YES;
    [_videoImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(player:)]];
    [self.contentView addSubview:_videoImageView];
    
     _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 205, 300, 20)];
    _titleLabel.font = [UIFont systemFontOfSize:15];
   // _titleLabel.backgroundColor = [UIColor brownColor];
    [self.contentView addSubview:_titleLabel];
    
    _desLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 225, 300, 10)];
    _desLabel.font =[UIFont systemFontOfSize:10];
   // _desLabel.backgroundColor = [UIColor grayColor];
    [self.contentView addSubview:_desLabel];
    
    _timeImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 240, 15, 15)];
    _timeImageView.image = [UIImage imageNamed:@"Time_16.png"];
    [self.contentView addSubview:_timeImageView];
    
    _playImageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 240, 15, 15)];
    _playImageView.image = [UIImage imageNamed:@"audivoisual"];
    [self.contentView addSubview:_playImageView];
    
    _editImageView = [[UIImageView alloc] initWithFrame:CGRectMake(200, 240, 15, 15)];
    _editImageView.image = [UIImage imageNamed:@"comment.png"];
    [self.contentView addSubview:_editImageView];
    
    _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(30, 240, 50, 15)];
    _timeLabel.font = [UIFont systemFontOfSize:15];
   // _timeLabel.backgroundColor = [UIColor blueColor];
    [self.contentView addSubview:_timeLabel];
    
    _playLabel = [[UILabel alloc] initWithFrame:CGRectMake(120, 240, 60, 15)];
    _playLabel.font = [UIFont systemFontOfSize:15];
   // _playLabel.backgroundColor = [UIColor greenColor];
    [self.contentView addSubview:_playLabel];
    
    _countLabel = [[UILabel alloc] initWithFrame:CGRectMake(220, 240, 60, 15)];
    _countLabel.font = [UIFont systemFontOfSize:15];
   // _countLabel.backgroundColor = [UIColor redColor];
    [self.contentView addSubview:_countLabel];
    
    _cellView = [[UIView alloc] initWithFrame:CGRectMake(0, 260, 375, 5)];
    _cellView.backgroundColor = [UIColor colorWithRed:231 / 256.000 green:231 / 256.000 blue:231 / 256.000 alpha:1.000];
    [self.contentView addSubview:_cellView];
    
    self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    self.playButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width / 2 -  20, 80, 40, 40);
    [self.playButton setBackgroundImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
    self.playButton.alpha = 0.5;
    self.playButton.userInteractionEnabled = YES;
    [self.playButton addTarget:self action:@selector(player:) forControlEvents:UIControlEventTouchUpInside];
    [self.videoImageView addSubview:self.playButton];
    
}


-(void)cellWithData:(VideoModel *)model
{
    _titleLabel.text = model.title;
    if (model.length % 60 < 10 && model.length / 60 < 10) {
        _timeLabel.text = [NSString stringWithFormat:@"0%ld:0%ld", model.length / 60, model.length % 60];
    } else if (model.length / 60 > 10 && model.length % 60 > 10) {
        _timeLabel.text = [NSString stringWithFormat:@"%ld:%ld", model.length / 60, model.length % 60];
    } else if (model.length % 60 < 10 && model.length / 60 > 10){
        _timeLabel.text = [NSString stringWithFormat:@"%ld:0%ld", model.length / 60, model.length % 60];
    } else if (model.length % 60 > 10 && model.length / 60 < 10) {
        _timeLabel.text = [NSString stringWithFormat:@"0%ld:%ld", model.length / 60, model.length % 60];
    }
    _playLabel.text = [NSString stringWithFormat:@"%ld",model.playCount];
    _countLabel.text = [NSString stringWithFormat:@"%ld",model.replyCount];
    [_videoImageView sd_setImageWithURL:[NSURL URLWithString:model.cover] placeholderImage:[UIImage imageNamed:@"1.png"]];
    self.urling = [NSURL URLWithString:model.mp4_url];
    _desLabel.text = model.MyDescription;
}

-(void)player:(UIButton *)button{
    self.audioVC = (AudiovisualViewController *)[self.superview.superview.superview.superview nextResponder];
    self.audioVC.cell = self;
    
    [self loadProgressHUD];
    [self.moviePlayer setContentURL:_urling];
    [self.progressHUD hide:YES];
    _moviePlayer.view.frame = self.videoImageView.bounds;
    _moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;  //属性的意思就是自动调整子控件与父控件中间的位置，宽高。
    [self.contentView addSubview:_moviePlayer.view];
    _moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
    [self.moviePlayer play];
    [self addNotification];
    
}

 //小菊花
-(void)loadProgressHUD{
    
    self.progressHUD = [[MBProgressHUD alloc] initWithView:self.moviePlayer.view];
    self.progressHUD.center = self.moviePlayer.view.center;
    self.progressHUD.backgroundColor = [UIColor grayColor];
    self.progressHUD.minSize = CGSizeMake(100, 100);
    self.progressHUD.alpha = 1;
    self.progressHUD.labelText = @"正在拼命加载中...";
    self.progressHUD.dimBackground = YES;
    self.progressHUD.square = YES;//设置显示框的高度和宽度一样
    self.progressHUD.frame = self.bounds;
    [self.moviePlayer.view addSubview:self.progressHUD];
    [self.progressHUD show:YES];
    
}
//注册通知
-(void)addNotification{
    NSNotificationCenter *notficationCenter = [NSNotificationCenter defaultCenter];
    [notficationCenter addObserver:self selector:@selector(mediaPlayerPlaybackStateChange1:) name:MPMoviePlayerWillEnterFullscreenNotification object:_moviePlayer];
    [notficationCenter addObserver:self selector:@selector(mediaPlayerPlaybackStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:_moviePlayer];
    
    [notficationCenter addObserver:self selector:@selector(mediaPlayerPlaybackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:_moviePlayer];
  
}

/*
*  播放状态改变,注意播放完成时的状态的暂停
 
*/

-(void)mediaPlayerPlaybackStateChange1:(NSNotification *)notfication{
    [[NSNotificationCenter defaultCenter] postNotificationName:MPMoviePlayerWillEnterFullscreenNotification object:self userInfo:@{@"isPlay":[NSNumber numberWithBool:YES]}];
}
//播放中
-(void)mediaPlayerPlaybackStateChange:(NSNotification *)notfication{
    switch (self.moviePlayer.playbackState) {
        case MPMoviePlaybackStatePlaying:
            NSLog(@"正在播放");
            break;
        case MPMoviePlaybackStatePaused:
            NSLog(@"暂停播放");
            break;
        case MPMoviePlaybackStateStopped:
            NSLog(@"停止播放");
            break;
        default:
            NSLog(@"播放状态:%li",self.moviePlayer.playbackState);
            break;
    }
}
//播放完成

-(void)mediaPlayerPlaybackFinished:(NSNotification *)notfication
{
    NSLog(@"播放完成.%li",self.moviePlayer.playbackState);
    //    [self.moviePlayer stop];
    MPMoviePlayerController* theMovie = [notfication object];
    //销毁播放通知
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
    [theMovie.view removeFromSuperview];
 
}






@end
